var express = require('express')
var path = require('path')
var morgan = require('morgan')
var favicon = require('serve-favicon')
var bodyParser = require('body-parser')
var cookieParser = require('cookie-parser')
var session = require('express-session')
var MongoSession = require('connect-mongo')(session)

var settings = require('./settings.js')

var app = express()

/* 基本配置 */

app.use(morgan('dev'))
app.use(cookieParser())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extend:false}))

app.use(session({
  secret: settings.cookieSecret,
  key: settings.db,
  cookie: {
    maxAge: 1000 * 60 //1分钟
  },
  resave: true,
  saveUninitialized: false
}))

app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1')
  if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/
  else  next();
});

app.use(express.static(path.join(__dirname, 'dist')))

app.engine('html', require('ejs').renderFile)
app.set('view engine', 'html')


var server = require('./servers/server.js')(app)

app.listen(8080, function (err, res) {
  console.log('正在监听8080端口......')
})
